package com.houweishi.module.region.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.houweishi.module.region.mapper.BizRegionMapper;
import com.houweishi.module.region.domain.BizRegion;
import com.houweishi.module.region.service.IBizRegionService;

/**
 * 自助入网-地区Service业务层处理
 * 
 * @author houweishi
 * @date 2023-02-28
 */
@Service
public class BizRegionServiceImpl implements IBizRegionService 
{
    @Autowired
    private BizRegionMapper bizRegionMapper;

    /**
     * 查询自助入网-地区
     * 
     * @param id 自助入网-地区主键
     * @return 自助入网-地区
     */
    @Override
    public BizRegion selectBizRegionById(Long id)
    {
        return bizRegionMapper.selectBizRegionById(id);
    }

    /**
     * 查询自助入网-地区列表
     * 
     * @param bizRegion 自助入网-地区
     * @return 自助入网-地区
     */
    @Override
    public List<BizRegion> selectBizRegionList(BizRegion bizRegion)
    {
        return bizRegionMapper.selectBizRegionList(bizRegion);
    }

    /**
     * 新增自助入网-地区
     * 
     * @param bizRegion 自助入网-地区
     * @return 结果
     */
    @Override
    public int insertBizRegion(BizRegion bizRegion)
    {
        return bizRegionMapper.insertBizRegion(bizRegion);
    }

    /**
     * 修改自助入网-地区
     * 
     * @param bizRegion 自助入网-地区
     * @return 结果
     */
    @Override
    public int updateBizRegion(BizRegion bizRegion)
    {
        return bizRegionMapper.updateBizRegion(bizRegion);
    }

    /**
     * 批量删除自助入网-地区
     * 
     * @param ids 需要删除的自助入网-地区主键
     * @return 结果
     */
    @Override
    public int deleteBizRegionByIds(Long[] ids)
    {
        return bizRegionMapper.deleteBizRegionByIds(ids);
    }

    /**
     * 删除自助入网-地区信息
     * 
     * @param id 自助入网-地区主键
     * @return 结果
     */
    @Override
    public int deleteBizRegionById(Long id)
    {
        return bizRegionMapper.deleteBizRegionById(id);
    }

    @Override
    public List<Map<String, Object>> selectTreeList() {
        //省集合
        List<BizRegion> ps=bizRegionMapper.selectProvinceList();

        //市集合
        List<BizRegion> cs=bizRegionMapper.selectCityList();
        //区县集合
        List<BizRegion> ds=bizRegionMapper.selectDistrictList();

        //封装省市区
        List<Map<String, Object>> arrayProvinceList = new ArrayList<Map<String, Object>>();
        //遍历
        for (BizRegion provinces : ps) {
            ArrayList arrayCityList = new ArrayList();
            Map<String, Object> provinceMap = new HashMap<>();
            //遍历
            for (BizRegion citys : cs) {
                if(citys.getCityCode().startsWith(provinces.getCode())){
                    ArrayList arrayDistrictList = new ArrayList();
                    Map<String, Object> cityMap = new HashMap<>();
                    //获取区/县列表
                    for (BizRegion districts : ds) {
                        if(districts.getDistrictCode().startsWith(citys.getCityCode())){
                            Map<String, String> districtMap = new HashMap<>();
                            districtMap.put("label", districts.getDistrictName());
                            districtMap.put("value", districts.getDistrictCode());
                            arrayDistrictList.add(districtMap);
                        }
                    }
                    cityMap.put("label", citys.getCityName());
                    cityMap.put("value", citys.getCityCode());
                    cityMap.put("children", arrayDistrictList);
                    arrayCityList.add(cityMap);
                }
            }
            provinceMap.put("label", provinces.getProvinceName());
            provinceMap.put("value", provinces.getCode());
            provinceMap.put("children", arrayCityList);
            arrayProvinceList.add(provinceMap);
        }
        return arrayProvinceList;
    }
}
